package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wgq
 * @date 2019-10-21 14:34
 * 获取登录账户名 模块
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 获取登录用户的用户名
     *
     * @return
     */
    @RequestMapping("/getUsername")
    public Result getUsername() {
        UserDetails user = null;
        try {
            //getAuthentication:认证信息
            //当SpringSecurity完成认证后，会将当前用户信息保存在框架提供的上下文对象
            user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (user != null) {
                return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);

    }

    /**
     * 登录用户查询  --> 角色查询
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 用户管理：新增用户数据
     * @param user
     * @param roleIds
     */
    //@PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds){
        try {
            userService.addUser(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL,e.getMessage());
        }
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }
    /**
     * 编辑窗口弹出，根据行 ID 回显数据
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        User user;
        try {
            user = userService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_USER_SUCCESS,user);

    }

    /**
     * 查询用户和角色之间的  对应关系
     * @param id
     * @return
     */
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){
        try {
            List<Integer> roleIds = userService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
    /**
     * 编辑用户
     * @param user
     * @param roleIds
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds){
        try {
            userService.edit(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL, e.getMessage());
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESSS);
    }

}
